package com.yuanfengerp.UserManage.service.impl;

import com.yuanfengerp.UserManage.dao.IUserManageDao;
import com.yuanfengerp.UserManage.model.UserManage;
import com.yuanfengerp.UserManage.service.IUserManageService;
import com.yuanfengerp.publicUtil.Result;
import com.yuanfengerp.publicUtil.PasswordUtil ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;

@Service
public class UserManageServiceImpl implements IUserManageService {
    @Autowired
    private IUserManageDao iumd;

    /**
     *  分页查询用户信息
     * @param pageSize
     * @param offset
     * @return
     */
    @Override
    public ArrayList<UserManage> getUserManage(int pageSize, int offset) {
        ArrayList<UserManage> list = iumd.getUserManage(pageSize,offset);
        return list;
    }

    @Override
    public Result<UserManage> getUser(int user_ID) {
        UserManage userManage = iumd.getUser(user_ID);
        if(userManage!=null){
            return Result.success(userManage);
        }
        return Result.failure(400,"查询失败");
    }

    @Override
    public Result<UserManage> addUser(UserManage user) {
//        if(user.getUser_name() == null){
//            return Result.failure(400,"用户名不能为空");
//        }
//        if(user.getUser_true_name() == null){
//            return Result.failure(400,"真实姓名不能为空");
//        }
//        if(user.getUser_password() == null){
//            return Result.failure(400,"密码不能为空");
//        }
        // 使用盐值 "yuanfeng" 进行加密
        String encryptedPassword = PasswordUtil.encryptPassword(user.getUser_password() + "yuanfeng");
        user.setUser_password(encryptedPassword);
        user.setUser_create_time(new Date());
        user.setUser_modification_time(new Date());
        int i = iumd.addUser(user);
        return i == 1 ? Result.success(user) : Result.failure(400,"添加失败");
    }

    @Override
    public Result<UserManage> updateUser(UserManage user) {
//        if(user.getUser_ID() <= 0){
//            return Result.failure(400,"用户ID不能为空");
//        }
        user.setUser_modification_time(new Date());
        int i = iumd.updateUser(user);
        return i == 1 ? Result.success(user) : Result.failure(400,"修改失败");
    }
}
